package cn.telling.telMarketCentre.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.telling.product.vo.TelMarketPubHis;
import cn.telling.telMarketCentre.dao.ITelMarketPubHisDao;
import cn.telling.utils.BaseUtil;
import cn.telling.utils.StringHelperTools;
/**
 * 
 * @ClassName: TelMarketPubHisDaoImpl 
 * @Description: 产品发布历史数据服务接口的实现
 * @author 陈赟 
 * @date 2015年6月17日 上午10:08:17 
 *
 */
@Repository("telMarketPubHisDao")
public class TelMarketPubHisDaoImpl implements ITelMarketPubHisDao {
	private static final Logger LOGGER = LoggerFactory.getLogger(TelMarketPubHisDaoImpl.class);
	
	@Autowired
	@Qualifier("film-template")
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 批量记录通信市场产品发布历史
	 */
	@Override
	public int[] insertTelMarketPubHis(final List<TelMarketPubHis> telMarketPubHisList) {
		final String sql = "insert into telMarket_pub_his(product_id,supplyId,pulishiTime,supplyAreaId,price,stockNum,add_date,add_user,update_date,update_user) values(?,?,sysdate,?,?,?,sysdate,?,sysdate,?) ";
		
		int[] count = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				TelMarketPubHis telMarketPubHis = telMarketPubHisList.get(i);
				ps.setBigDecimal(1, telMarketPubHis.getProduct_id());
				ps.setBigDecimal(2, telMarketPubHis.getSupplyId());
				ps.setBigDecimal(3, telMarketPubHis.getSupplyAreaId());
				ps.setBigDecimal(4, telMarketPubHis.getPrice());
				ps.setBigDecimal(5, telMarketPubHis.getStockNum());
				ps.setBigDecimal(6, telMarketPubHis.getAdd_user());
				ps.setBigDecimal(7, telMarketPubHis.getUpdate_user());
				
				Object[] args = new Object[]{ telMarketPubHis.getProduct_id(),telMarketPubHis.getSupplyId(),
						telMarketPubHis.getSupplyAreaId(),telMarketPubHis.getPrice(),telMarketPubHis.getStockNum(),
						telMarketPubHis.getAdd_user(),telMarketPubHis.getUpdate_user()
					 };
				args = StringHelperTools.nullToString(args);
				LOGGER.debug("批量插入telMarket_pub_his表的数据:"+BaseUtil.logSQL(sql, args));
			}
			
			@Override
			public int getBatchSize() {
				return telMarketPubHisList.size();
			}
		});
		
		return count;
	}
	/**
	 * 记录单个通信市场产品发布历史
	 */
	@Override
	public int insertTelMarketPubHis(TelMarketPubHis telMarketPubHis) {
		String sql = "insert into telMarket_pub_his(product_id,supplyId,pulishiTime,supplyAreaId,price,stockNum,add_date,add_user,update_date,update_user) values(?,?,sysdate,?,?,?,sysdate,?,sysdate,?) ";
		Object[] args = new Object[]{telMarketPubHis.getProduct_id(),telMarketPubHis.getSupplyId(),
					telMarketPubHis.getSupplyAreaId(),telMarketPubHis.getPrice(),
					telMarketPubHis.getStockNum(),telMarketPubHis.getAdd_user(),
					telMarketPubHis.getUpdate_user()
				};
		args = StringHelperTools.nullToString(args);
		
		LOGGER.debug("单个通信市场产品发布历史：" + BaseUtil.logSQL(sql, args));
		int count = jdbcTemplate.update(sql, args);
		return count;
	}
	/**
	 * 修改通信市场产品发布历史 截止日期
	 */
	@Override
	public int updateTelMarketPubHis(String productId, String supplyAreaId) {
		String sql = "update telMarket_pub_his set deadlineTime = sysdate where product_id = ? and supplyAreaId = ? and deadlineTime is null";
		Object[] args = new Object[]{productId,supplyAreaId};
		args = StringHelperTools.nullToString(args);
		
		LOGGER.debug("修改通信市场产品发布历史 截止日期：" + BaseUtil.logSQL(sql, args));
		int count = jdbcTemplate.update(sql, args);
		return count;
	}
	
	/**批量更新通信市场产品发布历史 截止日期*/
	@Override
	public int updateTelMarketPubHisBatch(List<String> productIdList, List<String> supplyAreaIdList) {
		String sql = "update telMarket_pub_his set deadlineTime = sysdate where product_id = ? and supplyAreaId = ? and deadlineTime is null";
		List<Object[]> paramsList = new ArrayList<Object[]>();
		for (int i = 0; i < supplyAreaIdList.size(); i++) {
			Object[] args = new Object[]{productIdList.get(i), supplyAreaIdList.get(i)};
			args = StringHelperTools.nullToString(args);
			
			paramsList.add(args);
			
	        LOGGER.debug("批量更新通讯市场产品发布历史截止日期:" +BaseUtil.logSQL(sql, new Object[]{}));
		}
		
		int count[] = jdbcTemplate.batchUpdate(sql,paramsList);
		
		return count.length;
	}
}
